
-- --------------------------------------------------
-- Entity Designer DDL Script for SQL Server 2005, 2008, and Azure
-- --------------------------------------------------
-- Date Created: 11/01/2011 01:27:23
-- Generated from EDMX file: C:\Diploma\Models\DiplomaModel.edmx
-- --------------------------------------------------

SET QUOTED_IDENTIFIER OFF;
GO
USE [Diploma];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_Administrators_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Administrators] DROP CONSTRAINT [FK_Administrators_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_CheckPoints_CheckPoints]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CheckPoints] DROP CONSTRAINT [FK_CheckPoints_CheckPoints];
GO
IF OBJECT_ID(N'[dbo].[FK_CollaborationRoomProposals_CollaborationRooms]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CollaborationProposes] DROP CONSTRAINT [FK_CollaborationRoomProposals_CollaborationRooms];
GO
IF OBJECT_ID(N'[dbo].[FK_CollaborationRoomProposals_Students]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CollaborationProposes] DROP CONSTRAINT [FK_CollaborationRoomProposals_Students];
GO
IF OBJECT_ID(N'[dbo].[FK_CollaborationRooms_Lecturers]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CollaborationRooms] DROP CONSTRAINT [FK_CollaborationRooms_Lecturers];
GO
IF OBJECT_ID(N'[dbo].[FK_CollaborationRooms_Lecturers1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[CollaborationRooms] DROP CONSTRAINT [FK_CollaborationRooms_Lecturers1];
GO
IF OBJECT_ID(N'[dbo].[FK_DepartmentHeads_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DepartmentHeads] DROP CONSTRAINT [FK_DepartmentHeads_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_DocumentInOD_OrderDiplom]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DocumentInOD] DROP CONSTRAINT [FK_DocumentInOD_OrderDiplom];
GO
IF OBJECT_ID(N'[dbo].[FK_DocumentInOD_VerifyDocuments]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DocumentInOD] DROP CONSTRAINT [FK_DocumentInOD_VerifyDocuments];
GO
IF OBJECT_ID(N'[dbo].[FK_DocumentInSOP_SubOrderPractice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DocumentInSOP] DROP CONSTRAINT [FK_DocumentInSOP_SubOrderPractice];
GO
IF OBJECT_ID(N'[dbo].[FK_DocumentInSOP_VerifyDocuments]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[DocumentInSOP] DROP CONSTRAINT [FK_DocumentInSOP_VerifyDocuments];
GO
IF OBJECT_ID(N'[dbo].[FK_GlobalTasks_CheckPoints]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GlobalTasks] DROP CONSTRAINT [FK_GlobalTasks_CheckPoints];
GO
IF OBJECT_ID(N'[dbo].[FK_GlobalTasks_PlaningTypes]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GlobalTasks] DROP CONSTRAINT [FK_GlobalTasks_PlaningTypes];
GO
IF OBJECT_ID(N'[dbo].[FK_GlobalTasks_QualificationLevels]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GlobalTasks] DROP CONSTRAINT [FK_GlobalTasks_QualificationLevels];
GO
IF OBJECT_ID(N'[dbo].[FK_GlobalTasksCompletions_Students]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GlobalTasksCompletions] DROP CONSTRAINT [FK_GlobalTasksCompletions_Students];
GO
IF OBJECT_ID(N'[dbo].[FK_GlobalTasksInRoomsCompletions_GlobalTasks]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[GlobalTasksCompletions] DROP CONSTRAINT [FK_GlobalTasksInRoomsCompletions_GlobalTasks];
GO
IF OBJECT_ID(N'[dbo].[FK_Lecturers_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Lecturers] DROP CONSTRAINT [FK_Lecturers_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_LecturersLoadings_Lecturers]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[LecturersLoadings] DROP CONSTRAINT [FK_LecturersLoadings_Lecturers];
GO
IF OBJECT_ID(N'[dbo].[FK_LecturersLoadings_QualificationLevels]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[LecturersLoadings] DROP CONSTRAINT [FK_LecturersLoadings_QualificationLevels];
GO
IF OBJECT_ID(N'[dbo].[FK_Messages_CollaborationRooms]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Messages_CollaborationRooms];
GO
IF OBJECT_ID(N'[dbo].[FK_Messages_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Messages] DROP CONSTRAINT [FK_Messages_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_NormControlDocs_Students]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NormControlDocs] DROP CONSTRAINT [FK_NormControlDocs_Students];
GO
IF OBJECT_ID(N'[dbo].[FK_NormControllers_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[NormControllers] DROP CONSTRAINT [FK_NormControllers_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_OrderDiplom_OrderStatus]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[OrderDiplom] DROP CONSTRAINT [FK_OrderDiplom_OrderStatus];
GO
IF OBJECT_ID(N'[dbo].[FK_PublicMessages_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[PublicMessages] DROP CONSTRAINT [FK_PublicMessages_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_RoomInOrderPractice_OrderPractice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RoomInOrderPractice] DROP CONSTRAINT [FK_RoomInOrderPractice_OrderPractice];
GO
IF OBJECT_ID(N'[dbo].[FK_RoomInOrderPractice_Students]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RoomInOrderPractice] DROP CONSTRAINT [FK_RoomInOrderPractice_Students];
GO
IF OBJECT_ID(N'[dbo].[FK_SaveStateOrderDiplom_OrderDiplom]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SaveStateOrderDiplom] DROP CONSTRAINT [FK_SaveStateOrderDiplom_OrderDiplom];
GO
IF OBJECT_ID(N'[dbo].[FK_SaveStateOrderPractice_OrderPractice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SaveStateOrderPractice] DROP CONSTRAINT [FK_SaveStateOrderPractice_OrderPractice];
GO
IF OBJECT_ID(N'[dbo].[FK_SSOPInSOP_SaveStateOrderPractice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SSOPInSOP] DROP CONSTRAINT [FK_SSOPInSOP_SaveStateOrderPractice];
GO
IF OBJECT_ID(N'[dbo].[FK_SSOPInSOP_SubOrderPractice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SSOPInSOP] DROP CONSTRAINT [FK_SSOPInSOP_SubOrderPractice];
GO
IF OBJECT_ID(N'[dbo].[FK_StudenstGroups_QualificationLevels]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[StudentsGroups] DROP CONSTRAINT [FK_StudenstGroups_QualificationLevels];
GO
IF OBJECT_ID(N'[dbo].[FK_Students_StudenstGroups]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Students] DROP CONSTRAINT [FK_Students_StudenstGroups];
GO
IF OBJECT_ID(N'[dbo].[FK_Students_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Students] DROP CONSTRAINT [FK_Students_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_SubOrderPractice_OrderPractice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SubOrderPractice] DROP CONSTRAINT [FK_SubOrderPractice_OrderPractice];
GO
IF OBJECT_ID(N'[dbo].[FK_SubOrderPractice_OrderStatus]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SubOrderPractice] DROP CONSTRAINT [FK_SubOrderPractice_OrderStatus];
GO
IF OBJECT_ID(N'[dbo].[FK_SubOrderToOrderDiplom_OrderDiplom]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SubOrderToOrderDiplom] DROP CONSTRAINT [FK_SubOrderToOrderDiplom_OrderDiplom];
GO
IF OBJECT_ID(N'[dbo].[FK_SubOrderToOrderDiplom_SubOrderPractice]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SubOrderToOrderDiplom] DROP CONSTRAINT [FK_SubOrderToOrderDiplom_SubOrderPractice];
GO
IF OBJECT_ID(N'[dbo].[FK_SystemManagers_Users]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[SystemManagers] DROP CONSTRAINT [FK_SystemManagers_Users];
GO
IF OBJECT_ID(N'[dbo].[FK_Tasks_CheckPoints]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Tasks] DROP CONSTRAINT [FK_Tasks_CheckPoints];
GO
IF OBJECT_ID(N'[dbo].[FK_Tasks_CollaborationRooms1]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Tasks] DROP CONSTRAINT [FK_Tasks_CollaborationRooms1];
GO
IF OBJECT_ID(N'[dbo].[FK_Tasks_GlobalTasks]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[Tasks] DROP CONSTRAINT [FK_Tasks_GlobalTasks];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[Administrators]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Administrators];
GO
IF OBJECT_ID(N'[dbo].[CheckPoints]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CheckPoints];
GO
IF OBJECT_ID(N'[dbo].[CollaborationProposes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CollaborationProposes];
GO
IF OBJECT_ID(N'[dbo].[CollaborationRooms]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CollaborationRooms];
GO
IF OBJECT_ID(N'[dbo].[DepartmentHeads]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DepartmentHeads];
GO
IF OBJECT_ID(N'[dbo].[DocumentInOD]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DocumentInOD];
GO
IF OBJECT_ID(N'[dbo].[DocumentInSOP]', 'U') IS NOT NULL
    DROP TABLE [dbo].[DocumentInSOP];
GO
IF OBJECT_ID(N'[dbo].[GlobalTasks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GlobalTasks];
GO
IF OBJECT_ID(N'[dbo].[GlobalTasksCompletions]', 'U') IS NOT NULL
    DROP TABLE [dbo].[GlobalTasksCompletions];
GO
IF OBJECT_ID(N'[dbo].[Lecturers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Lecturers];
GO
IF OBJECT_ID(N'[dbo].[LecturersLoadings]', 'U') IS NOT NULL
    DROP TABLE [dbo].[LecturersLoadings];
GO
IF OBJECT_ID(N'[dbo].[Messages]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Messages];
GO
IF OBJECT_ID(N'[dbo].[NormControlDocs]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NormControlDocs];
GO
IF OBJECT_ID(N'[dbo].[NormControllers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[NormControllers];
GO
IF OBJECT_ID(N'[dbo].[OrderDiplom]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderDiplom];
GO
IF OBJECT_ID(N'[dbo].[OrderPractice]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderPractice];
GO
IF OBJECT_ID(N'[dbo].[OrderStatus]', 'U') IS NOT NULL
    DROP TABLE [dbo].[OrderStatus];
GO
IF OBJECT_ID(N'[dbo].[PlaningTypes]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PlaningTypes];
GO
IF OBJECT_ID(N'[dbo].[PublicMessages]', 'U') IS NOT NULL
    DROP TABLE [dbo].[PublicMessages];
GO
IF OBJECT_ID(N'[dbo].[QualificationLevels]', 'U') IS NOT NULL
    DROP TABLE [dbo].[QualificationLevels];
GO
IF OBJECT_ID(N'[dbo].[RoomInOrderPractice]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RoomInOrderPractice];
GO
IF OBJECT_ID(N'[dbo].[SaveStateOrderDiplom]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SaveStateOrderDiplom];
GO
IF OBJECT_ID(N'[dbo].[SaveStateOrderPractice]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SaveStateOrderPractice];
GO
IF OBJECT_ID(N'[dbo].[SSOPInSOP]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SSOPInSOP];
GO
IF OBJECT_ID(N'[dbo].[Students]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Students];
GO
IF OBJECT_ID(N'[dbo].[StudentsGroups]', 'U') IS NOT NULL
    DROP TABLE [dbo].[StudentsGroups];
GO
IF OBJECT_ID(N'[dbo].[SubOrderPractice]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SubOrderPractice];
GO
IF OBJECT_ID(N'[dbo].[SubOrderToOrderDiplom]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SubOrderToOrderDiplom];
GO
IF OBJECT_ID(N'[dbo].[SystemManagers]', 'U') IS NOT NULL
    DROP TABLE [dbo].[SystemManagers];
GO
IF OBJECT_ID(N'[dbo].[Tasks]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Tasks];
GO
IF OBJECT_ID(N'[dbo].[Users]', 'U') IS NOT NULL
    DROP TABLE [dbo].[Users];
GO
IF OBJECT_ID(N'[dbo].[VerifyDocuments]', 'U') IS NOT NULL
    DROP TABLE [dbo].[VerifyDocuments];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'CheckPoints'
CREATE TABLE [dbo].[CheckPoints] (
    [CheckPointId] uniqueidentifier  NOT NULL,
    [Name] nvarchar(150)  NOT NULL,
    [StartDate] datetime  NULL,
    [EndDate] datetime  NULL,
    [PrevCheckPointId] uniqueidentifier  NULL,
    [Description] varchar(max)  NULL,
    [RemindInDays] int  NOT NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'Messages'
CREATE TABLE [dbo].[Messages] (
    [MessageId] uniqueidentifier  NOT NULL,
    [UserId] uniqueidentifier  NOT NULL,
    [CollaborationRoomId] uniqueidentifier  NULL,
    [Text] nvarchar(max)  NOT NULL,
    [PubDate] datetime  NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'PlaningTypes'
CREATE TABLE [dbo].[PlaningTypes] (
    [PlaningTypeId] int  NOT NULL,
    [Name] nvarchar(50)  NOT NULL
);
GO

-- Creating table 'QualificationLevels'
CREATE TABLE [dbo].[QualificationLevels] (
    [QualificationLevelId] int  NOT NULL,
    [Name] nchar(10)  NOT NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'Users'
CREATE TABLE [dbo].[Users] (
    [UserId] uniqueidentifier  NOT NULL,
    [Email] varchar(70)  NOT NULL,
    [Password] nvarchar(128)  NOT NULL,
    [Name] nvarchar(50)  NOT NULL,
    [Surname] nvarchar(50)  NOT NULL,
    [Patronymic] nvarchar(50)  NOT NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'LecturersLoadings'
CREATE TABLE [dbo].[LecturersLoadings] (
    [LecturerId] uniqueidentifier  NOT NULL,
    [QualificationLevelId] int  NOT NULL,
    [StudentsAmount] int  NOT NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'CollaborationRooms'
CREATE TABLE [dbo].[CollaborationRooms] (
    [CollaborationRoomId] uniqueidentifier  NOT NULL,
    [LecturerId] uniqueidentifier  NOT NULL,
    [ConsultantId] uniqueidentifier  NULL,
    [DateAdd] datetime  NULL,
    [BaseTheme] nvarchar(max)  NOT NULL,
    [BaseDescription] varchar(max)  NULL,
    [PractivePlace] varchar(max)  NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'CollaborationProposes'
CREATE TABLE [dbo].[CollaborationProposes] (
    [StudentId] uniqueidentifier  NOT NULL,
    [CollaborationRoomId] uniqueidentifier  NULL,
    [DateTime] datetime  NOT NULL,
    [Theme] nvarchar(max)  NULL,
    [Description] varchar(max)  NULL,
    [IsApproved] bit  NOT NULL,
    [DateApproved] datetime  NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'StudentsGroups'
CREATE TABLE [dbo].[StudentsGroups] (
    [StudentsGroupId] uniqueidentifier  NOT NULL,
    [QualificationLevelId] int  NULL,
    [Name] nvarchar(50)  NOT NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'OrderPractices'
CREATE TABLE [dbo].[OrderPractices] (
    [Id] uniqueidentifier  NOT NULL,
    [CreateDate] datetime  NULL,
    [UpdateDate] datetime  NULL,
    [IsVerify] bit  NULL,
    [Name] nvarchar(max)  NULL,
    [StartDate] datetime  NULL,
    [StopDate] datetime  NULL
);
GO

-- Creating table 'OrderStatus'
CREATE TABLE [dbo].[OrderStatus] (
    [Id] uniqueidentifier  NOT NULL,
    [Value] int  NOT NULL,
    [DisplayName] nvarchar(max)  NOT NULL
);
GO

-- Creating table 'SaveStateOrderPractices'
CREATE TABLE [dbo].[SaveStateOrderPractices] (
    [Id] uniqueidentifier  NOT NULL,
    [OrderPracticeId] uniqueidentifier  NOT NULL,
    [Num] int  NOT NULL,
    [Fio] nvarchar(max)  NOT NULL,
    [StudentGroup] nvarchar(max)  NOT NULL,
    [PracticePlace] nvarchar(max)  NOT NULL,
    [LecturerPractice] nvarchar(max)  NOT NULL,
    [Theme] nvarchar(max)  NOT NULL,
    [LecturerDiplom] nvarchar(max)  NOT NULL,
    [Note] nvarchar(max)  NOT NULL,
    [Counsultant] nvarchar(max)  NOT NULL,
    [StartDate] datetime  NOT NULL,
    [StopDate] datetime  NOT NULL
);
GO

-- Creating table 'SubOrderPractices'
CREATE TABLE [dbo].[SubOrderPractices] (
    [Id] uniqueidentifier  NOT NULL,
    [OrderPracticeId] uniqueidentifier  NOT NULL,
    [CreateDate] datetime  NULL,
    [UpdateDate] datetime  NULL,
    [BookName] nvarchar(max)  NULL,
    [Title] nvarchar(max)  NULL,
    [Text] nvarchar(max)  NULL,
    [StatusId] uniqueidentifier  NULL,
    [StartDate] datetime  NULL,
    [StopDate] datetime  NULL
);
GO

-- Creating table 'VerifyDocuments'
CREATE TABLE [dbo].[VerifyDocuments] (
    [Id] uniqueidentifier  NOT NULL,
    [DocumentURL] nvarchar(max)  NULL
);
GO

-- Creating table 'OrderDiploms'
CREATE TABLE [dbo].[OrderDiploms] (
    [Id] uniqueidentifier  NOT NULL,
    [CreateDate] datetime  NULL,
    [UpdateDate] datetime  NULL,
    [StatusId] uniqueidentifier  NOT NULL,
    [BookName] nvarchar(max)  NULL,
    [Title] nvarchar(max)  NULL,
    [Text] nvarchar(max)  NULL
);
GO

-- Creating table 'SaveStateOrderDiploms'
CREATE TABLE [dbo].[SaveStateOrderDiploms] (
    [Id] uniqueidentifier  NOT NULL,
    [OrderDiplomId] uniqueidentifier  NOT NULL,
    [Num] int  NOT NULL,
    [Fio] nvarchar(max)  NOT NULL,
    [StudentGroup] nvarchar(max)  NOT NULL,
    [Lecturer] nvarchar(max)  NOT NULL,
    [Consultant] nvarchar(max)  NOT NULL,
    [Theme] nvarchar(max)  NOT NULL,
    [Note] nvarchar(max)  NOT NULL,
    [StartDate] datetime  NOT NULL,
    [EndDate] datetime  NOT NULL
);
GO

-- Creating table 'GlobalTasksCompletions'
CREATE TABLE [dbo].[GlobalTasksCompletions] (
    [GlobalTaskId] uniqueidentifier  NOT NULL,
    [StudentId] uniqueidentifier  NOT NULL,
    [IsComplete] bit  NOT NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'PublicMessages'
CREATE TABLE [dbo].[PublicMessages] (
    [PublicMessageId] uniqueidentifier  NOT NULL,
    [UserId] uniqueidentifier  NOT NULL,
    [Message] nvarchar(max)  NOT NULL,
    [AttachedFilePath] nvarchar(max)  NULL,
    [PubDate] datetime  NOT NULL,
    [DeletionDate] datetime  NULL
);
GO

-- Creating table 'NormControlDocs'
CREATE TABLE [dbo].[NormControlDocs] (
    [NormControlDocId] uniqueidentifier  NOT NULL,
    [IsPassed] bit  NOT NULL,
    [StudentId] uniqueidentifier  NOT NULL,
    [StudentNotes] nvarchar(max)  NULL,
    [ControllerNotes] nvarchar(max)  NULL,
    [FileName] nvarchar(max)  NOT NULL,
    [DocFile] varbinary(max)  NOT NULL,
    [PdfFile] varbinary(max)  NOT NULL,
    [DateSanded] datetime  NOT NULL
);
GO

-- Creating table 'CheckPoints_GlobalTask'
CREATE TABLE [dbo].[CheckPoints_GlobalTask] (
    [QualificationLevelId] int  NULL,
    [PlaningTypeId] int  NOT NULL,
    [CheckPointId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'CheckPoints_Task'
CREATE TABLE [dbo].[CheckPoints_Task] (
    [GlobalTaskId] uniqueidentifier  NOT NULL,
    [CollaborationRoomId] uniqueidentifier  NULL,
    [IsCompleted] bit  NOT NULL,
    [CheckPointId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Users_Lecturer'
CREATE TABLE [dbo].[Users_Lecturer] (
    [Rank] nvarchar(100)  NULL,
    [UserId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Users_Student'
CREATE TABLE [dbo].[Users_Student] (
    [StudentsGroupId] uniqueidentifier  NULL,
    [InformationAbout] nvarchar(max)  NULL,
    [CollaborationProposeId] uniqueidentifier  NULL,
    [UserId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Users_Administrator'
CREATE TABLE [dbo].[Users_Administrator] (
    [UserId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Users_SystemManager'
CREATE TABLE [dbo].[Users_SystemManager] (
    [UserId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Users_DepartmentHead'
CREATE TABLE [dbo].[Users_DepartmentHead] (
    [UserId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'Users_NormController'
CREATE TABLE [dbo].[Users_NormController] (
    [UserId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'DocumentInSOP'
CREATE TABLE [dbo].[DocumentInSOP] (
    [SubOrderPractices_Id] uniqueidentifier  NOT NULL,
    [VerifyDocuments_Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'RoomInOrderPractice'
CREATE TABLE [dbo].[RoomInOrderPractice] (
    [OrderPractices_Id] uniqueidentifier  NOT NULL,
    [Students_UserId] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'SSOPInSOP'
CREATE TABLE [dbo].[SSOPInSOP] (
    [SaveStateOrderPractices_Id] uniqueidentifier  NOT NULL,
    [SubOrderPractices_Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'DocumentInOD'
CREATE TABLE [dbo].[DocumentInOD] (
    [OrderDiploms_Id] uniqueidentifier  NOT NULL,
    [VerifyDocuments_Id] uniqueidentifier  NOT NULL
);
GO

-- Creating table 'SubOrderToOrderDiplom'
CREATE TABLE [dbo].[SubOrderToOrderDiplom] (
    [OrderDiploms_Id] uniqueidentifier  NOT NULL,
    [SubOrderPractices_Id] uniqueidentifier  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [CheckPointId] in table 'CheckPoints'
ALTER TABLE [dbo].[CheckPoints]
ADD CONSTRAINT [PK_CheckPoints]
    PRIMARY KEY CLUSTERED ([CheckPointId] ASC);
GO

-- Creating primary key on [MessageId] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [PK_Messages]
    PRIMARY KEY CLUSTERED ([MessageId] ASC);
GO

-- Creating primary key on [PlaningTypeId] in table 'PlaningTypes'
ALTER TABLE [dbo].[PlaningTypes]
ADD CONSTRAINT [PK_PlaningTypes]
    PRIMARY KEY CLUSTERED ([PlaningTypeId] ASC);
GO

-- Creating primary key on [QualificationLevelId] in table 'QualificationLevels'
ALTER TABLE [dbo].[QualificationLevels]
ADD CONSTRAINT [PK_QualificationLevels]
    PRIMARY KEY CLUSTERED ([QualificationLevelId] ASC);
GO

-- Creating primary key on [UserId] in table 'Users'
ALTER TABLE [dbo].[Users]
ADD CONSTRAINT [PK_Users]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [LecturerId], [QualificationLevelId] in table 'LecturersLoadings'
ALTER TABLE [dbo].[LecturersLoadings]
ADD CONSTRAINT [PK_LecturersLoadings]
    PRIMARY KEY CLUSTERED ([LecturerId], [QualificationLevelId] ASC);
GO

-- Creating primary key on [CollaborationRoomId] in table 'CollaborationRooms'
ALTER TABLE [dbo].[CollaborationRooms]
ADD CONSTRAINT [PK_CollaborationRooms]
    PRIMARY KEY CLUSTERED ([CollaborationRoomId] ASC);
GO

-- Creating primary key on [StudentId] in table 'CollaborationProposes'
ALTER TABLE [dbo].[CollaborationProposes]
ADD CONSTRAINT [PK_CollaborationProposes]
    PRIMARY KEY CLUSTERED ([StudentId] ASC);
GO

-- Creating primary key on [StudentsGroupId] in table 'StudentsGroups'
ALTER TABLE [dbo].[StudentsGroups]
ADD CONSTRAINT [PK_StudentsGroups]
    PRIMARY KEY CLUSTERED ([StudentsGroupId] ASC);
GO

-- Creating primary key on [Id] in table 'OrderPractices'
ALTER TABLE [dbo].[OrderPractices]
ADD CONSTRAINT [PK_OrderPractices]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OrderStatus'
ALTER TABLE [dbo].[OrderStatus]
ADD CONSTRAINT [PK_OrderStatus]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'SaveStateOrderPractices'
ALTER TABLE [dbo].[SaveStateOrderPractices]
ADD CONSTRAINT [PK_SaveStateOrderPractices]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'SubOrderPractices'
ALTER TABLE [dbo].[SubOrderPractices]
ADD CONSTRAINT [PK_SubOrderPractices]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'VerifyDocuments'
ALTER TABLE [dbo].[VerifyDocuments]
ADD CONSTRAINT [PK_VerifyDocuments]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'OrderDiploms'
ALTER TABLE [dbo].[OrderDiploms]
ADD CONSTRAINT [PK_OrderDiploms]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'SaveStateOrderDiploms'
ALTER TABLE [dbo].[SaveStateOrderDiploms]
ADD CONSTRAINT [PK_SaveStateOrderDiploms]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [GlobalTaskId], [StudentId] in table 'GlobalTasksCompletions'
ALTER TABLE [dbo].[GlobalTasksCompletions]
ADD CONSTRAINT [PK_GlobalTasksCompletions]
    PRIMARY KEY CLUSTERED ([GlobalTaskId], [StudentId] ASC);
GO

-- Creating primary key on [PublicMessageId] in table 'PublicMessages'
ALTER TABLE [dbo].[PublicMessages]
ADD CONSTRAINT [PK_PublicMessages]
    PRIMARY KEY CLUSTERED ([PublicMessageId] ASC);
GO

-- Creating primary key on [NormControlDocId] in table 'NormControlDocs'
ALTER TABLE [dbo].[NormControlDocs]
ADD CONSTRAINT [PK_NormControlDocs]
    PRIMARY KEY CLUSTERED ([NormControlDocId] ASC);
GO

-- Creating primary key on [CheckPointId] in table 'CheckPoints_GlobalTask'
ALTER TABLE [dbo].[CheckPoints_GlobalTask]
ADD CONSTRAINT [PK_CheckPoints_GlobalTask]
    PRIMARY KEY CLUSTERED ([CheckPointId] ASC);
GO

-- Creating primary key on [CheckPointId] in table 'CheckPoints_Task'
ALTER TABLE [dbo].[CheckPoints_Task]
ADD CONSTRAINT [PK_CheckPoints_Task]
    PRIMARY KEY CLUSTERED ([CheckPointId] ASC);
GO

-- Creating primary key on [UserId] in table 'Users_Lecturer'
ALTER TABLE [dbo].[Users_Lecturer]
ADD CONSTRAINT [PK_Users_Lecturer]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [UserId] in table 'Users_Student'
ALTER TABLE [dbo].[Users_Student]
ADD CONSTRAINT [PK_Users_Student]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [UserId] in table 'Users_Administrator'
ALTER TABLE [dbo].[Users_Administrator]
ADD CONSTRAINT [PK_Users_Administrator]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [UserId] in table 'Users_SystemManager'
ALTER TABLE [dbo].[Users_SystemManager]
ADD CONSTRAINT [PK_Users_SystemManager]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [UserId] in table 'Users_DepartmentHead'
ALTER TABLE [dbo].[Users_DepartmentHead]
ADD CONSTRAINT [PK_Users_DepartmentHead]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [UserId] in table 'Users_NormController'
ALTER TABLE [dbo].[Users_NormController]
ADD CONSTRAINT [PK_Users_NormController]
    PRIMARY KEY CLUSTERED ([UserId] ASC);
GO

-- Creating primary key on [SubOrderPractices_Id], [VerifyDocuments_Id] in table 'DocumentInSOP'
ALTER TABLE [dbo].[DocumentInSOP]
ADD CONSTRAINT [PK_DocumentInSOP]
    PRIMARY KEY NONCLUSTERED ([SubOrderPractices_Id], [VerifyDocuments_Id] ASC);
GO

-- Creating primary key on [OrderPractices_Id], [Students_UserId] in table 'RoomInOrderPractice'
ALTER TABLE [dbo].[RoomInOrderPractice]
ADD CONSTRAINT [PK_RoomInOrderPractice]
    PRIMARY KEY NONCLUSTERED ([OrderPractices_Id], [Students_UserId] ASC);
GO

-- Creating primary key on [SaveStateOrderPractices_Id], [SubOrderPractices_Id] in table 'SSOPInSOP'
ALTER TABLE [dbo].[SSOPInSOP]
ADD CONSTRAINT [PK_SSOPInSOP]
    PRIMARY KEY NONCLUSTERED ([SaveStateOrderPractices_Id], [SubOrderPractices_Id] ASC);
GO

-- Creating primary key on [OrderDiploms_Id], [VerifyDocuments_Id] in table 'DocumentInOD'
ALTER TABLE [dbo].[DocumentInOD]
ADD CONSTRAINT [PK_DocumentInOD]
    PRIMARY KEY NONCLUSTERED ([OrderDiploms_Id], [VerifyDocuments_Id] ASC);
GO

-- Creating primary key on [OrderDiploms_Id], [SubOrderPractices_Id] in table 'SubOrderToOrderDiplom'
ALTER TABLE [dbo].[SubOrderToOrderDiplom]
ADD CONSTRAINT [PK_SubOrderToOrderDiplom]
    PRIMARY KEY NONCLUSTERED ([OrderDiploms_Id], [SubOrderPractices_Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [PlaningTypeId] in table 'CheckPoints_GlobalTask'
ALTER TABLE [dbo].[CheckPoints_GlobalTask]
ADD CONSTRAINT [FK_GlobalTasks_PlaningTypes]
    FOREIGN KEY ([PlaningTypeId])
    REFERENCES [dbo].[PlaningTypes]
        ([PlaningTypeId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GlobalTasks_PlaningTypes'
CREATE INDEX [IX_FK_GlobalTasks_PlaningTypes]
ON [dbo].[CheckPoints_GlobalTask]
    ([PlaningTypeId]);
GO

-- Creating foreign key on [QualificationLevelId] in table 'CheckPoints_GlobalTask'
ALTER TABLE [dbo].[CheckPoints_GlobalTask]
ADD CONSTRAINT [FK_GlobalTasks_QualificationLevels]
    FOREIGN KEY ([QualificationLevelId])
    REFERENCES [dbo].[QualificationLevels]
        ([QualificationLevelId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GlobalTasks_QualificationLevels'
CREATE INDEX [IX_FK_GlobalTasks_QualificationLevels]
ON [dbo].[CheckPoints_GlobalTask]
    ([QualificationLevelId]);
GO

-- Creating foreign key on [UserId] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Messages_Users]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Messages_Users'
CREATE INDEX [IX_FK_Messages_Users]
ON [dbo].[Messages]
    ([UserId]);
GO

-- Creating foreign key on [GlobalTaskId] in table 'CheckPoints_Task'
ALTER TABLE [dbo].[CheckPoints_Task]
ADD CONSTRAINT [FK_Tasks_GlobalTasks]
    FOREIGN KEY ([GlobalTaskId])
    REFERENCES [dbo].[CheckPoints_GlobalTask]
        ([CheckPointId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Tasks_GlobalTasks'
CREATE INDEX [IX_FK_Tasks_GlobalTasks]
ON [dbo].[CheckPoints_Task]
    ([GlobalTaskId]);
GO

-- Creating foreign key on [LecturerId] in table 'LecturersLoadings'
ALTER TABLE [dbo].[LecturersLoadings]
ADD CONSTRAINT [FK_LecturersLoadings_Lecturers]
    FOREIGN KEY ([LecturerId])
    REFERENCES [dbo].[Users_Lecturer]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [QualificationLevelId] in table 'LecturersLoadings'
ALTER TABLE [dbo].[LecturersLoadings]
ADD CONSTRAINT [FK_LecturersLoadings_QualificationLevels]
    FOREIGN KEY ([QualificationLevelId])
    REFERENCES [dbo].[QualificationLevels]
        ([QualificationLevelId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_LecturersLoadings_QualificationLevels'
CREATE INDEX [IX_FK_LecturersLoadings_QualificationLevels]
ON [dbo].[LecturersLoadings]
    ([QualificationLevelId]);
GO

-- Creating foreign key on [LecturerId] in table 'CollaborationRooms'
ALTER TABLE [dbo].[CollaborationRooms]
ADD CONSTRAINT [FK_CollaborationRooms_Lecturers]
    FOREIGN KEY ([LecturerId])
    REFERENCES [dbo].[Users_Lecturer]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CollaborationRooms_Lecturers'
CREATE INDEX [IX_FK_CollaborationRooms_Lecturers]
ON [dbo].[CollaborationRooms]
    ([LecturerId]);
GO

-- Creating foreign key on [ConsultantId] in table 'CollaborationRooms'
ALTER TABLE [dbo].[CollaborationRooms]
ADD CONSTRAINT [FK_CollaborationRooms_Lecturers1]
    FOREIGN KEY ([ConsultantId])
    REFERENCES [dbo].[Users_Lecturer]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CollaborationRooms_Lecturers1'
CREATE INDEX [IX_FK_CollaborationRooms_Lecturers1]
ON [dbo].[CollaborationRooms]
    ([ConsultantId]);
GO

-- Creating foreign key on [CollaborationRoomId] in table 'Messages'
ALTER TABLE [dbo].[Messages]
ADD CONSTRAINT [FK_Messages_CollaborationRooms]
    FOREIGN KEY ([CollaborationRoomId])
    REFERENCES [dbo].[CollaborationRooms]
        ([CollaborationRoomId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Messages_CollaborationRooms'
CREATE INDEX [IX_FK_Messages_CollaborationRooms]
ON [dbo].[Messages]
    ([CollaborationRoomId]);
GO

-- Creating foreign key on [CollaborationRoomId] in table 'CheckPoints_Task'
ALTER TABLE [dbo].[CheckPoints_Task]
ADD CONSTRAINT [FK_Tasks_CollaborationRooms1]
    FOREIGN KEY ([CollaborationRoomId])
    REFERENCES [dbo].[CollaborationRooms]
        ([CollaborationRoomId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Tasks_CollaborationRooms1'
CREATE INDEX [IX_FK_Tasks_CollaborationRooms1]
ON [dbo].[CheckPoints_Task]
    ([CollaborationRoomId]);
GO

-- Creating foreign key on [CollaborationRoomId] in table 'CollaborationProposes'
ALTER TABLE [dbo].[CollaborationProposes]
ADD CONSTRAINT [FK_CollaborationRoomProposals_CollaborationRooms]
    FOREIGN KEY ([CollaborationRoomId])
    REFERENCES [dbo].[CollaborationRooms]
        ([CollaborationRoomId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CollaborationRoomProposals_CollaborationRooms'
CREATE INDEX [IX_FK_CollaborationRoomProposals_CollaborationRooms]
ON [dbo].[CollaborationProposes]
    ([CollaborationRoomId]);
GO

-- Creating foreign key on [StudentId] in table 'CollaborationProposes'
ALTER TABLE [dbo].[CollaborationProposes]
ADD CONSTRAINT [FK_CollaborationRoomProposals_Students]
    FOREIGN KEY ([StudentId])
    REFERENCES [dbo].[Users_Student]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [QualificationLevelId] in table 'StudentsGroups'
ALTER TABLE [dbo].[StudentsGroups]
ADD CONSTRAINT [FK_StudentsGroups_QualificationLevels1]
    FOREIGN KEY ([QualificationLevelId])
    REFERENCES [dbo].[QualificationLevels]
        ([QualificationLevelId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_StudentsGroups_QualificationLevels1'
CREATE INDEX [IX_FK_StudentsGroups_QualificationLevels1]
ON [dbo].[StudentsGroups]
    ([QualificationLevelId]);
GO

-- Creating foreign key on [StudentsGroupId] in table 'Users_Student'
ALTER TABLE [dbo].[Users_Student]
ADD CONSTRAINT [FK_Students_StudentsGroups1]
    FOREIGN KEY ([StudentsGroupId])
    REFERENCES [dbo].[StudentsGroups]
        ([StudentsGroupId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_Students_StudentsGroups1'
CREATE INDEX [IX_FK_Students_StudentsGroups1]
ON [dbo].[Users_Student]
    ([StudentsGroupId]);
GO

-- Creating foreign key on [PrevCheckPointId] in table 'CheckPoints'
ALTER TABLE [dbo].[CheckPoints]
ADD CONSTRAINT [FK_CheckPoints_CheckPoints]
    FOREIGN KEY ([PrevCheckPointId])
    REFERENCES [dbo].[CheckPoints]
        ([CheckPointId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_CheckPoints_CheckPoints'
CREATE INDEX [IX_FK_CheckPoints_CheckPoints]
ON [dbo].[CheckPoints]
    ([PrevCheckPointId]);
GO

-- Creating foreign key on [OrderPracticeId] in table 'SaveStateOrderPractices'
ALTER TABLE [dbo].[SaveStateOrderPractices]
ADD CONSTRAINT [FK_SaveStateOrderPractice_OrderPractice]
    FOREIGN KEY ([OrderPracticeId])
    REFERENCES [dbo].[OrderPractices]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SaveStateOrderPractice_OrderPractice'
CREATE INDEX [IX_FK_SaveStateOrderPractice_OrderPractice]
ON [dbo].[SaveStateOrderPractices]
    ([OrderPracticeId]);
GO

-- Creating foreign key on [OrderPracticeId] in table 'SubOrderPractices'
ALTER TABLE [dbo].[SubOrderPractices]
ADD CONSTRAINT [FK_SubOrderPractice_OrderPractice]
    FOREIGN KEY ([OrderPracticeId])
    REFERENCES [dbo].[OrderPractices]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SubOrderPractice_OrderPractice'
CREATE INDEX [IX_FK_SubOrderPractice_OrderPractice]
ON [dbo].[SubOrderPractices]
    ([OrderPracticeId]);
GO

-- Creating foreign key on [StatusId] in table 'SubOrderPractices'
ALTER TABLE [dbo].[SubOrderPractices]
ADD CONSTRAINT [FK_SubOrderPractice_OrderStatus]
    FOREIGN KEY ([StatusId])
    REFERENCES [dbo].[OrderStatus]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SubOrderPractice_OrderStatus'
CREATE INDEX [IX_FK_SubOrderPractice_OrderStatus]
ON [dbo].[SubOrderPractices]
    ([StatusId]);
GO

-- Creating foreign key on [SubOrderPractices_Id] in table 'DocumentInSOP'
ALTER TABLE [dbo].[DocumentInSOP]
ADD CONSTRAINT [FK_DocumentInSOP_SubOrderPractice]
    FOREIGN KEY ([SubOrderPractices_Id])
    REFERENCES [dbo].[SubOrderPractices]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [VerifyDocuments_Id] in table 'DocumentInSOP'
ALTER TABLE [dbo].[DocumentInSOP]
ADD CONSTRAINT [FK_DocumentInSOP_VerifyDocument]
    FOREIGN KEY ([VerifyDocuments_Id])
    REFERENCES [dbo].[VerifyDocuments]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DocumentInSOP_VerifyDocument'
CREATE INDEX [IX_FK_DocumentInSOP_VerifyDocument]
ON [dbo].[DocumentInSOP]
    ([VerifyDocuments_Id]);
GO

-- Creating foreign key on [OrderPractices_Id] in table 'RoomInOrderPractice'
ALTER TABLE [dbo].[RoomInOrderPractice]
ADD CONSTRAINT [FK_RoomInOrderPractice_OrderPractice]
    FOREIGN KEY ([OrderPractices_Id])
    REFERENCES [dbo].[OrderPractices]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [Students_UserId] in table 'RoomInOrderPractice'
ALTER TABLE [dbo].[RoomInOrderPractice]
ADD CONSTRAINT [FK_RoomInOrderPractice_Student]
    FOREIGN KEY ([Students_UserId])
    REFERENCES [dbo].[Users_Student]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_RoomInOrderPractice_Student'
CREATE INDEX [IX_FK_RoomInOrderPractice_Student]
ON [dbo].[RoomInOrderPractice]
    ([Students_UserId]);
GO

-- Creating foreign key on [SaveStateOrderPractices_Id] in table 'SSOPInSOP'
ALTER TABLE [dbo].[SSOPInSOP]
ADD CONSTRAINT [FK_SSOPInSOP_SaveStateOrderPractice]
    FOREIGN KEY ([SaveStateOrderPractices_Id])
    REFERENCES [dbo].[SaveStateOrderPractices]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [SubOrderPractices_Id] in table 'SSOPInSOP'
ALTER TABLE [dbo].[SSOPInSOP]
ADD CONSTRAINT [FK_SSOPInSOP_SubOrderPractice]
    FOREIGN KEY ([SubOrderPractices_Id])
    REFERENCES [dbo].[SubOrderPractices]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SSOPInSOP_SubOrderPractice'
CREATE INDEX [IX_FK_SSOPInSOP_SubOrderPractice]
ON [dbo].[SSOPInSOP]
    ([SubOrderPractices_Id]);
GO

-- Creating foreign key on [StatusId] in table 'OrderDiploms'
ALTER TABLE [dbo].[OrderDiploms]
ADD CONSTRAINT [FK_OrderDiplom_OrderStatus]
    FOREIGN KEY ([StatusId])
    REFERENCES [dbo].[OrderStatus]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_OrderDiplom_OrderStatus'
CREATE INDEX [IX_FK_OrderDiplom_OrderStatus]
ON [dbo].[OrderDiploms]
    ([StatusId]);
GO

-- Creating foreign key on [OrderDiplomId] in table 'SaveStateOrderDiploms'
ALTER TABLE [dbo].[SaveStateOrderDiploms]
ADD CONSTRAINT [FK_SaveStateOrderDiplom_OrderDiplom]
    FOREIGN KEY ([OrderDiplomId])
    REFERENCES [dbo].[OrderDiploms]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SaveStateOrderDiplom_OrderDiplom'
CREATE INDEX [IX_FK_SaveStateOrderDiplom_OrderDiplom]
ON [dbo].[SaveStateOrderDiploms]
    ([OrderDiplomId]);
GO

-- Creating foreign key on [OrderDiploms_Id] in table 'DocumentInOD'
ALTER TABLE [dbo].[DocumentInOD]
ADD CONSTRAINT [FK_DocumentInOD_OrderDiplom]
    FOREIGN KEY ([OrderDiploms_Id])
    REFERENCES [dbo].[OrderDiploms]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [VerifyDocuments_Id] in table 'DocumentInOD'
ALTER TABLE [dbo].[DocumentInOD]
ADD CONSTRAINT [FK_DocumentInOD_VerifyDocument]
    FOREIGN KEY ([VerifyDocuments_Id])
    REFERENCES [dbo].[VerifyDocuments]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_DocumentInOD_VerifyDocument'
CREATE INDEX [IX_FK_DocumentInOD_VerifyDocument]
ON [dbo].[DocumentInOD]
    ([VerifyDocuments_Id]);
GO

-- Creating foreign key on [GlobalTaskId] in table 'GlobalTasksCompletions'
ALTER TABLE [dbo].[GlobalTasksCompletions]
ADD CONSTRAINT [FK_GlobalTasksInRoomsCompletions_GlobalTasks1]
    FOREIGN KEY ([GlobalTaskId])
    REFERENCES [dbo].[CheckPoints_GlobalTask]
        ([CheckPointId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [StudentId] in table 'GlobalTasksCompletions'
ALTER TABLE [dbo].[GlobalTasksCompletions]
ADD CONSTRAINT [FK_GlobalTasksCompletions_Students]
    FOREIGN KEY ([StudentId])
    REFERENCES [dbo].[Users_Student]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_GlobalTasksCompletions_Students'
CREATE INDEX [IX_FK_GlobalTasksCompletions_Students]
ON [dbo].[GlobalTasksCompletions]
    ([StudentId]);
GO

-- Creating foreign key on [UserId] in table 'PublicMessages'
ALTER TABLE [dbo].[PublicMessages]
ADD CONSTRAINT [FK_PublicMessages_Users]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_PublicMessages_Users'
CREATE INDEX [IX_FK_PublicMessages_Users]
ON [dbo].[PublicMessages]
    ([UserId]);
GO

-- Creating foreign key on [OrderDiploms_Id] in table 'SubOrderToOrderDiplom'
ALTER TABLE [dbo].[SubOrderToOrderDiplom]
ADD CONSTRAINT [FK_SubOrderToOrderDiplom_OrderDiplom]
    FOREIGN KEY ([OrderDiploms_Id])
    REFERENCES [dbo].[OrderDiploms]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [SubOrderPractices_Id] in table 'SubOrderToOrderDiplom'
ALTER TABLE [dbo].[SubOrderToOrderDiplom]
ADD CONSTRAINT [FK_SubOrderToOrderDiplom_SubOrderPractice]
    FOREIGN KEY ([SubOrderPractices_Id])
    REFERENCES [dbo].[SubOrderPractices]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_SubOrderToOrderDiplom_SubOrderPractice'
CREATE INDEX [IX_FK_SubOrderToOrderDiplom_SubOrderPractice]
ON [dbo].[SubOrderToOrderDiplom]
    ([SubOrderPractices_Id]);
GO

-- Creating foreign key on [StudentId] in table 'NormControlDocs'
ALTER TABLE [dbo].[NormControlDocs]
ADD CONSTRAINT [FK_NormControlDocs_Students]
    FOREIGN KEY ([StudentId])
    REFERENCES [dbo].[Users_Student]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_NormControlDocs_Students'
CREATE INDEX [IX_FK_NormControlDocs_Students]
ON [dbo].[NormControlDocs]
    ([StudentId]);
GO

-- Creating foreign key on [CheckPointId] in table 'CheckPoints_GlobalTask'
ALTER TABLE [dbo].[CheckPoints_GlobalTask]
ADD CONSTRAINT [FK_GlobalTask_inherits_CheckPoint]
    FOREIGN KEY ([CheckPointId])
    REFERENCES [dbo].[CheckPoints]
        ([CheckPointId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [CheckPointId] in table 'CheckPoints_Task'
ALTER TABLE [dbo].[CheckPoints_Task]
ADD CONSTRAINT [FK_Task_inherits_CheckPoint]
    FOREIGN KEY ([CheckPointId])
    REFERENCES [dbo].[CheckPoints]
        ([CheckPointId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [UserId] in table 'Users_Lecturer'
ALTER TABLE [dbo].[Users_Lecturer]
ADD CONSTRAINT [FK_Lecturer_inherits_User]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [UserId] in table 'Users_Student'
ALTER TABLE [dbo].[Users_Student]
ADD CONSTRAINT [FK_Student_inherits_User]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [UserId] in table 'Users_Administrator'
ALTER TABLE [dbo].[Users_Administrator]
ADD CONSTRAINT [FK_Administrator_inherits_User]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [UserId] in table 'Users_SystemManager'
ALTER TABLE [dbo].[Users_SystemManager]
ADD CONSTRAINT [FK_SystemManager_inherits_User]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [UserId] in table 'Users_DepartmentHead'
ALTER TABLE [dbo].[Users_DepartmentHead]
ADD CONSTRAINT [FK_DepartmentHead_inherits_User]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- Creating foreign key on [UserId] in table 'Users_NormController'
ALTER TABLE [dbo].[Users_NormController]
ADD CONSTRAINT [FK_NormController_inherits_User]
    FOREIGN KEY ([UserId])
    REFERENCES [dbo].[Users]
        ([UserId])
    ON DELETE NO ACTION ON UPDATE NO ACTION;
GO

-- --------------------------------------------------
-- Script has ended
-- --------------------------------------------------